$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'monitor/notification/page',
        datatype: "json",
        colModel: [
            { label: '渠道ID', name: 'id', index: "id", width: 45, key: true, hidden:true },
			{ label: '渠道名称', name: 'name', width: 45 },
			{ label: '渠道类型', name: 'type', width: 75 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.records",
            page: "page.page",
            total: "page.pages",
            records: "page.total"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            name: null // 查询条件：渠道名称
        },
        showList: true,
        title:null,
        notification:{
            id: null, // 渠道id
            name: null, // 渠道名称
            type: "email", // 渠道类型
            ifDefault: "0",
            sendReminder: "0", // 是否启用告警压缩
            frequency: null, // 告警最高发送频率
            settings: null
        },
        emailSettings: "", // email告警渠道settings
        // webhook告警渠道settings
        webhookSettings: {
            url: ""
        },
        // dingtalk告警渠道settings
        dingtalkSettings: {
            url: null, // dingtalk webhook地址
            secret: null, // dingtalk 机器人密钥
            messageType: "text", // dingtalk消息类型
            isAtAll: "0", // 是否at所有人
            mobiles: "" // 被at成员的手机号，多个手机号使用英文逗号隔开
        },
        addOrUpdate: "add",
        typeList: [
            { value: "email", label: "email", disabled: false},
            { value: "webhook", label: "webhook", disabled: false },
            { value: "dingtalk", label: "dingtalk", disabled: false },
            { value: "sms", label: "sms", disabled: true },
            { value: "wechat", label: "wechat", disabled: true }
        ],
        dingtalkMsgTypeList: [
            {value: "text", label: "文本模式"},
            {value: "markdown", label: "markdown模式"},
            {value: "text", label: "文本模式"}
        ]
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            this.addOrUpdate = "add";
            vm.showList = false;
            vm.title = "新增";
            vm.initialNotificationSettings();
            $("#typeSelect").selectpicker('val', vm.notification.type);
            $("#dingtalkMsgType").selectpicker('val', vm.dingtalkSettings.messageType);
        },
        // 修改
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getNotification(id);
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "monitor/notification/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            switch (vm.notification.type) {
                case "email":
                    vm.notification.settings = vm.emailSettings
                    break;
                case "webhook":
                    vm.notification.settings = JSON.stringify(vm.webhookSettings);
                    break;
                case "dingtalk":
                    vm.notification.settings = JSON.stringify(vm.dingtalkSettings);
                    break;
                default:
                    break;
            }
            var url = vm.notification.id == null ? "monitor/notification/add" : "monitor/notification/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.notification),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
        },
        // 编辑：根据id获取告警渠道信息
        getNotification: function(id){
            $.get(baseURL + "monitor/notification/"+id, function(r){
                vm.initialNotificationSettings();
                vm.notification = r.notification;
                // 根据告警类型转换settings为对应对象
                switch (vm.notification.type) {
                    case "email":
                        vm.emailSettings = vm.notification.settings;
                        break;
                    case "webhook":
                        vm.webhookSettings = JSON.parse(vm.notification.settings);
                        break;
                    case "dingtalk":
                        vm.dingtalkSettings = JSON.parse(vm.notification.settings)
                        break;
                    default:
                        break;
                }
                $("#typeSelect").selectpicker('val', vm.notification.type);
            });
        },
        initialNotificationSettings: function () {
            vm.emailSettings = "";
            vm.webhookSettings = {url: ""}
            vm.dingtalkSettings = {url: null,secret: null,messageType: "text",isAtAll: "0",mobiles: ""}
            vm.notification={id: null,name: null,type: "email",isDefault: "0",sendReminder: "0",frequency: null,settings: null
            };
        }
    }
});